/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author atikahSH
 * Kelas ini berfungsi untuk mengelola fungsi yang dibutuhkan dalam modul Order
 * Fungsi tersebut dihungunkan dengan kelas entitas HistoryOrder yang merupakan represntasi dari tabel orders dalam database
 */
public class HistoryOrderQuery {
    /* menghubungkan dengan database sesuai dengan
     * konfigurasi pada persistence unit
     */

    public HistoryOrderQuery() {
        emf = Persistence.createEntityManagerFactory("persistence");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public List<HistoryOrder> getHistoryOrders(Obat obat) {
        List<HistoryOrder> historyOrders = new ArrayList<HistoryOrder>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM history_order AS o WHERE o.idOrder=:ie ORDER BY o.idOrder DESC");
            q.setParameter("ie", historyOrders);
            historyOrders = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return historyOrders;
    }

    /* method untuk memasukkan satu baris Order baru ke dalam tabel historyOrders
     */
    public void createHistoryOrder(HistoryOrder historyOrder) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(historyOrder);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public boolean checkHistoryOrder(Integer idOrder) {
        boolean result = false;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT count(o) FROM history_order AS o WHERE o.idOrder=:ie");
            q.setParameter("ie", idOrder);
            int jumlahOrder = ((Long) q.getSingleResult()).intValue();
            if (jumlahOrder > 0) {
                result = true;
            }
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return result;
    }
    /* method yang digunakan untuk mengecek keberadaan satu baris
     * pada tabel Orders berdasarkan parameter namaOrder
     */

    public boolean checkOrder(String namaOrder) {
        boolean result = false;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT count(o) FROM history_order AS o WHERE o.namaOrder=:ne");
            q.setParameter("ne", namaOrder);
            int jumlahOrder = ((Long) q.getSingleResult()).intValue();
            if (jumlahOrder > 0) {
                result = true;
            }
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return result;
    }

    /* method untuk mengambil satu baris Order
     * berdasarkan parameter idOrder
     */
    public HistoryOrder getOrder(Integer idOrder) {
        HistoryOrder HistoryOrder = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM history_order AS o WHERE o.idOrder=:ie");
            q.setParameter("ie", idOrder);
            HistoryOrder = (HistoryOrder) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return HistoryOrder;
    }
    /* method untuk mengambil satu baris Order
     * berdasarkan parameter namaOrder
     */

    public HistoryOrder getOrder(String namaOrder) {
        HistoryOrder HistoryOrder = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM history_order AS o WHERE o.namaOrder=:ne");
            q.setParameter("ne", namaOrder);
            HistoryOrder = (HistoryOrder) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return HistoryOrder;
    }

    /* method untuk mengambil semua baris Order dari tabel Orders berdasarkan User yang sedang login
     */
    public List<HistoryOrder> getMyOrders(User user) {
        List<HistoryOrder> Orders = new ArrayList<HistoryOrder>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM history_order AS o WHERE o.idUser=:usr ORDER BY o.idOrder DESC");
            q.setParameter("usr", user);
            Orders = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return Orders;
    }
    /* method untuk mengambil semua baris Order dari tabel Orders berdasarkan User yang sedang login
     */

    public List<HistoryOrder> findOrders(String namaOrder) {
        List<HistoryOrder> Orders = new ArrayList<HistoryOrder>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM history_order AS o WHERE o.namaOrder LIKE :pattern ORDER BY o.idOrder DESC");
            q.setParameter("pattern", "%"+namaOrder+"%");
            Orders = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return Orders;
    }

    /* method untuk mengambil semua baris Order dari tabel Orders diurutkan berdasarkan data yang terbaru
     */
    public List<HistoryOrder> getAllOrders() {
        List<HistoryOrder> Orders = new ArrayList<HistoryOrder>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM history_order AS o ORDER BY o.idOrder DESC");
            Orders = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return Orders;
    }

    /* method untuk mengambil lima baris awal Order dari tabel Orders diurutkan berdasarkan data yang terbaru
     */
    public List<HistoryOrder> getLimitedOrders() {
        List<HistoryOrder> Orders = new ArrayList<HistoryOrder>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM history_order AS o ORDER BY o.idOrder DESC");
            q.setMaxResults(5);
            Orders = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return Orders;
    }

    /* method untuk mengganti nilai atribut pada satu baris Order yang sudah ada pada tabel Orders
     */
    public void editOrder(HistoryOrder Order) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.merge(Order);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }


    /* method untuk menghapus satu baris Order pada tabel historyOrders
     */
    public void deleteHistoryOrder(int id) throws NonexistentEntityException {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            HistoryOrder historyOrder;
            try {
                historyOrder = em.getReference(HistoryOrder.class, id);
                historyOrder.getIdOrder();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The historyOrder with id " + id + " no longer exists.", enfe);
            }
            em.remove(historyOrder);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}
